import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;
/**
 * The UI class behind the Store class that implements Store and all of its actions
 * 
 * @author Srinidhi Chaganty
 */

public class StorePanel extends TransactionPanel{
	/*
	 * player is a Player object
	 * store is a Store object
	 * totalRep is returned by generateStore
	 * storeRep is a JPanel representation of the Store
	 * sellRep is a JPanel representation of the Store's sell screen
	 * buyRep is a JPanel representation of the Store's buy screen
	 * mule is a JPanel that is returned by buyMule
	 */
	private Player player;
	private Store store;
	private JPanel totalRep;
	private JPanel storeRep;
	private JPanel sellRep;
	private JPanel buyRep;
	private boolean endPlacing;
	private JPanel mule; 
	private Map m;
	
	/**
	 * Creates a StorePanel given a store 
	 * @param s The store passed in
	 */
	public StorePanel(Store s){
		store = s;
		m = GameModel.getInstance().getMap();
		player = GameModel.getInstance().getCurrPlayer();
		setEndPlacing(false);
	}
	
	/**
	 * The intro label for store
	 * 
	 * @return JLabel the JLabel with the intro
	 */
	@Override
	public JLabel introLabel() {
		return new JLabel("Welcome to the store what do you want to do?");
	}

	/**
	 * Generates the panel for selling resources
	 * @return JPanel the panel for selling resources
	 */
	public JPanel generateSell(){
		/*sellRep is returned by this method
		 * buttons is a JPanel that holds all the things to do (sell mule or any resource)
		 * sLab is a JLabel that prompts the user
		 * r1 is a button that, when clicked, will sell one Food resource
		 */
		JPanel sellRep = new JPanel(new BorderLayout());
		JPanel buttons = new JPanel(new GridLayout(3,1));
		JLabel sLab = new JLabel("What would you like to sell?");
		final JButton r1 = new JButton("Food: $30");
		r1.setBackground(Color.MAGENTA);
		r1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r1){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.sell(0,player)){
						//message is a String that tells that the user bought that resource
						String message = "Player " + player.getName() + " has successfully sold" +
								" food to the Store. \nPlayer " + player.getName() + " now has $" +
										+ player.getMoney();
						JOptionPane.showMessageDialog(frame,message);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You do not have that resource");
					}
				}
			}});
		//r2 is a button that, when clicked, will sell one Energy resource
		final JButton r2 = new JButton("Energy : $25");
		r2.setBackground(Color.GREEN);
		r2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r2){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.sell(1,player)){
						//message is a String that tells that the user bought that resource
						String message = "Player " + player.getName() + " has successfully sold" +
								" energy to the Store. \nPlayer " + player.getName() + " now has $" +
										+ player.getMoney();
						JOptionPane.showMessageDialog(frame,message);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You do not have that resource");
					}
				}
			}});
		//r3 is a button that, when clicked, will sell one Smithore resource
		final JButton r3 = new JButton("Smithore $50");
		r3.setBackground(Color.CYAN);
		r3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r3){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.sell(2,player)){
						//message is a String that tells the user that it bought that resource
						String message = "Player " + player.getName() + " has successfully sold" +
								" smithore to the Store. \nPlayer " + player.getName() + " now has $" +
										+ player.getMoney();
						JOptionPane.showMessageDialog(frame,message);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You do not have that resource");
					}
				}
			}});
		buttons.add(r1);
		buttons.add(r2);
		buttons.add(r3);
		sellRep.add(sLab, BorderLayout.NORTH);
		sellRep.add(buttons, BorderLayout.CENTER);
		return sellRep;
	}
	/**
	 * returns the panel for buying resources/mules
	 * @return JPanel the panel for buying resources/mules
	 */
	public JPanel generateBuy(){
		/*
		 * buyRep is returned by this method
		 * intro is a JPanel that holds buyLabel
		 * buyLabel is a JLabel that prompts the user
		 * muleButton is a button that goes to the mule screen if clicked
		 */
		final JPanel buyRep = new JPanel(new BorderLayout());
		final JPanel intro = new JPanel();
		JLabel buyLabel = new JLabel("What do you want to buy?");
		final JButton muleButton = new JButton("Mule");
		muleButton.setBackground(Color.YELLOW);
		muleButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == muleButton){
					intro.setVisible(false);
					//mule is a JPanel representation of the mule screen
					try {
						final JPanel mule = buyMule();
						//back is a button that goes back to the buy screen if clicked
						final JButton back = new JButton("Back to Buy Screen");
						back.setBackground(Color.YELLOW);
						back.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent e) {
								if(e.getSource() == back){
									mule.setVisible(false);
									intro.setVisible(true);
								}
							}});
						mule.add(back,BorderLayout.PAGE_END);
						buyRep.add(mule,BorderLayout.CENTER);
						mule.setVisible(true);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}});
		//resourceButto is a button that goes to the buy resources screen if clicked
		final JButton resourceButton = new JButton("Resources");
		resourceButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == resourceButton){
					intro.setVisible(false);
					//resource is a JPanel representation for the resource screen
					final JPanel resource = buyResource();
					//back is a button that goes back to the buy screen if clicked
					final JButton back = new JButton("Back to Buy Screen");
					back.setBackground(Color.YELLOW);
					back.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							if(e.getSource() == back){
								resource.setVisible(false);
								intro.setVisible(true);
							}
						}});
					resource.add(back,BorderLayout.PAGE_END);
					buyRep.add(resource,BorderLayout.CENTER);
					resource.setVisible(true);
				}
			}});
		resourceButton.setBackground(Color.ORANGE);
		intro.add(buyLabel);
		intro.add(muleButton);
		intro.add(resourceButton);
		buyRep.add(intro, BorderLayout.CENTER);
		return buyRep;
	}
	
	/**
	 * returns the panel for buying mules
	 * @return JPanel the panel for buying mules
	 * @throws IOException 
	 */
	public JPanel buyMule() throws IOException{
		mule = new JPanel(new BorderLayout());
		/*
		 * buttons is a JPanel that holds all the options (buy the different types of MULEs)
		 * muleLab is a JLabel that prompts the user
		 * totalMule is a JPanel that is returned by the method
		 * mule1 is a button that buys the first type of MUlE if clicked
		 */
		JPanel buttons = new JPanel(new GridLayout(3,1));
		JLabel muleLab = new JLabel("What type of MULE do you want?");
		final JPanel totalMule = new JPanel(new BorderLayout());
		final PicButton mule1 = new PicButton(getClass().getResource("StoreImages/FoodMule.png"));
		try {
			final JPanel mule1Place = placeMulePan(3);
			final JPanel mule2Place = placeMulePan(4);
			final JPanel mule3Place = placeMulePan(5);
		
		mule1.setBackground(Color.MAGENTA);
		mule1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == mule1){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(3,player)){
						store.buy(player, 3);
						//message1 is a successful purchase message
						String message1 = "Player " + player.getName() + " has succesfully " +
								"purchased a MULE with a food resource. \nIt has been added to their " +
								"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
						//message2 notifies the player that he needs to place the mule on a property
						String message2 = "Since you have purchased a MULE, please place it on a property" +
								"\nIf the MULE  is not placed on your property, it will be deleted.";
						JOptionPane.showMessageDialog(frame, message1);
						JOptionPane.showMessageDialog(frame, message2);
						player.addMule(new Mule("food", player));
						totalMule.setVisible(false);
						mule.add(mule1Place);
						mule1Place.setVisible(true);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		//mule2 is a button that buys the second type of MUlE if clicked
		final PicButton mule2 = new PicButton(getClass().getResource("StoreImages/EnergyMule.png"));
		mule2.setBackground(Color.GREEN);
		mule2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == mule2){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(4,player)){
						store.buy(player, 4);
						//message1 is a successful purchase message
						String message1 = "Player " + player.getName() + " has succesfully " +
								"purchased a MULE with an energy resource. \nIt has been added to their " +
								"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
						//message2 notifies the player that he needs to place the mule on a property
						String message2 = "Since you have purchased a MULE, please place it on a property" +
								"\nIf the MULE  is not placed on your property, it will be deleted.";
						JOptionPane.showMessageDialog(frame, message1);
						JOptionPane.showMessageDialog(frame, message2);
						player.addMule(new Mule("energy", player));
						totalMule.setVisible(false);
						mule.add(mule2Place);
						mule2Place.setVisible(true);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		//mule3 is a button that buys the third type of MUlE if clicked
		final PicButton mule3 = new PicButton(getClass().getResource("StoreImages/OreMule.png"));
		mule3.setBackground(Color.CYAN);
		mule3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == mule3){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(5,player)){
						store.buy(player, 5);
						//message1 is a successful purchase message
						String message1 = "Player " + player.getName() + " has succesfully " +
								"purchased a MULE with a smithore resource. \nIt has been added to their " +
								"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
						//message2 notifies the player that he needs to place the mule on a property
						String message2 = "Since you have purchased a MULE, please place it on a property" +
								"\nIf the MULE  is not placed on your property, it will be deleted.";
						JOptionPane.showMessageDialog(frame, message1);
						JOptionPane.showMessageDialog(frame, message2);
						player.addMule(new Mule("smithOre", player));
						totalMule.setVisible(false);
						mule.add(mule3Place);
						mule3Place.setVisible(true);
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		buttons.add(mule1);
		buttons.add(mule2);
		buttons.add(mule3);
		} catch (IOException e1) {
			e1.printStackTrace(); }
		totalMule.add(muleLab, BorderLayout.PAGE_START);
		totalMule.add(buttons,BorderLayout.CENTER);
		mule.add(totalMule);
		return mule;
	}
	
	private JPanel placeMulePan(int typeMule) throws IOException{
		final JPanel placeMule = m.generatePanel();
		PicButton[] arr = m.getButtonArr();
		final int type = typeMule;
		for(int i = 0; i < 45; i++){
			final PicButton j = arr[i];
			final Property prop = m.getProperty(i);
			if(prop.getTerrainNum() != 6){
				j.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						JFrame frame = new JFrame();
						if(prop.getOwner()!= null && prop.getOwner().getName().equals(player.getName())){
							prop.setHasMule(true);
							prop.setMule(new Mule(type, player));
							JOptionPane.showMessageDialog(frame,"Placed the mule!");
							placeMule.setVisible(false);
						}
						else{
							JOptionPane.showMessageDialog(frame,"Not your property- MULE deleted");
							placeMule.setVisible(false);
						}
					}
					
				}); 
			}
		}
		return placeMule;
	}
	
	/**
	 * returns the panel for buying resources
	 * @return JPanel the panel for buying resources
	 */
	public JPanel buyResource(){
		/*
		 * resource is returned by this method
		 * buttons is a JPanel that holds all the options (buy different types of resources)
		 * muleLab is a JLabel that prompts the user
		 * r1 is a button that, when clicked, will purchase one food resource
		 */
		JPanel resource = new JPanel(new BorderLayout());
		JPanel buttons = new JPanel(new GridLayout(3,1));
		JLabel muleLab = new JLabel("What type of resource do you want?");
		final JButton r1 = new JButton("Food: $30");
		r1.setBackground(Color.MAGENTA);
		r1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r1){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(0,player)){
						if(store.buy(player, 0)){
							//message1 is a successful purchase message
							String message1 = "Player " + player.getName() + " has succesfully " +
									"purchased a food resource. \nIt has been added to their " +
									"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
							JOptionPane.showMessageDialog(frame, message1);
						}
						else JOptionPane.showMessageDialog(frame, "Sold out!");
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		//r2 is a button that, when clicked, will purchase one energy resource
		final JButton r2 = new JButton("Energy : $25");
		r2.setBackground(Color.GREEN);
		r2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r2){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(1,player)){
						if(store.buy(player, 1)){
							//message1 is a successful purchase message
							String message1 = "Player " + player.getName() + " has succesfully " +
									"purchased an energy resource. \nIt has been added to their " +
									"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
							JOptionPane.showMessageDialog(frame, message1);
						}
						else JOptionPane.showMessageDialog(frame, "Sold out!"); 
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		//r3 is a button that, when clicked, will purchase one smithore resource
		final JButton r3 = new JButton("Smithore $50");
		r3.setBackground(Color.CYAN);
		r3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == r3){
					//frame is a new frame (used to for JOptionPaneMessage used later)
					JFrame frame = new JFrame();
					if(store.canAfford(2,player)){
						if(store.buy(player, 2)){
							//message is a successful purchase message
							String message1 = "Player " + player.getName() + " has succesfully " +
									"purchased a smithore resource. \nIt has been added to their " +
									"inventory. \nPlayer " + player.getName() + " now has $" + player.getMoney();
							JOptionPane.showMessageDialog(frame, message1);
						}
						else JOptionPane.showMessageDialog(frame, "Sold out!");
					}
					else{
						JOptionPane.showMessageDialog(frame,"You can not afford that- transaction cancelled");
					}
				}
			}});
		buttons.add(r1);
		buttons.add(r2);
		buttons.add(r3);
		resource.add(muleLab, BorderLayout.PAGE_START);
		resource.add(buttons,BorderLayout.CENTER);
		return resource;
	}
	/**
	 * Checks if the storePanel should be removed
	 * @return boolean if the store panel should be removed
	 */
	public boolean isEndPlacing() {
		return endPlacing;
	}
	/**
	 * Sets the endStore boolean to value passed in
	 * @param endStore the new value for endStore
	 */
	public void setEndPlacing (boolean endPlacing) {
		this.endPlacing = endPlacing;
	}
}
